#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import seafileapi
import lzma
import logging
log = logging.getLogger(__name__)
import argparse
import os
import re
import time

from seafile_io import SeaFileIo, searchfile

def main():
    parser = argparse.ArgumentParser(description="Upload files to seafile.")

    parser.add_argument('--fetch', dest = 'fetch_file', help='Input fetch files.("Full file name")')
    parser.add_argument('--server', dest='server_url', help='Input the server url.')
    parser.add_argument('--username', dest='user_name', help='Input the username.')
    parser.add_argument('--password', dest='password', help='Input the password.')
    parser.add_argument('--fetch_dir', dest='fetch_dir', help='Input the fetch dir.', default="")
    parser.add_argument('--repo', dest='repo_name', help='Input the repo name.', default="私人资料库")

    args = parser.parse_args()

    seafile_client = SeaFileIo(args.server_url, args.user_name, args.password, args.repo_name)

    if args.fetch_file != None:
       seafile_client.download_last_file('/' + args.fetch_dir.split('/',1)[-1], args.fetch_file)
    else:
       seafile_client.download_dir('/' + args.fetch_dir.split('/',1)[-1])

if __name__ == '__main__':
    log = logging.getLogger("")
    formatter = logging.Formatter("%(asctime)s %(levelname)s " + "[%(module)s:%(lineno)d] %(message)s")
    # setup console logging
    log.setLevel(logging.INFO)
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)

    ch.setFormatter(formatter)
    log.addHandler(ch)

    main()
